package com.shangcheng.server.controller;

import com.shangcheng.model.domain.UmsMenu;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.UmsMenuVo;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.service.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 新建数据
     *
     * @param umsMenu
     * @return
     */
    @PostMapping("create")
    public Result create(@RequestBody UmsMenu umsMenu) {
        umsMenuService.create(umsMenu);
        return new Result(200, null, "操作成功");
    }

    /**
     * 根据id进行数据的删除
     *
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean falg = umsMenuService.delete(id);
        return new Result(200, null, falg?"删除成功":"删除失败");
    }

    /**
     * 根据id进行数据的查找
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id) {
        UmsMenu umsMenu = umsMenuService.findById(id);
        return new Result(200, umsMenu, "");
    }

    /**
     * 根据id进行数据的修改
     *
     * @param id
     * @return
     */
    @PostMapping("update/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody UmsMenu umsMenu) {
        umsMenuService.update(id, umsMenu);
        return new Result(200, null, "");
    }

    /**
     * 根据id进行数据的修改
     *
     * @param id
     * @return
     */
    @PostMapping("updateHidden/{id}")
    public Result updateHidden(@PathVariable("id") Integer id, Integer hidden) {
        umsMenuService.updateHidden(id, hidden);
        return new Result(200, null, "");
    }

    /**
     * 分页查询后台菜单
     *
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{parentId}")
    public Result findPageMenu(@PathVariable("parentId") Integer parentId,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pr = umsMenuService.pageList(parentId, pageNum, pageSize);
        return new Result(200, pr, "");
    }

    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("treeList")
    public Result treeList() {
        List<UmsMenuVo> list = umsMenuService.treeList();
        return new Result(200, list, "");
    }
}
